package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.constants.user.UserConstants;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.base.BaseServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign.ArticleFeign;
import com.usian.user.feign.WmUserFeign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealNameMapper;
import com.usian.user.service.ApUserRealNameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ApUserRealNameServiceImpl extends BaseServiceImpl<ApUserRealNameMapper, ApUserRealname> implements ApUserRealNameService {

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;//此地注入Mapper


    //查看申请列表
    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        /*
        * 1.判断传入参数是否为空
        * 2.对参数getStatus(状态进行判断)、用lambda查到传入dto.getStatus == 数据库中字段的信息
        * 3.返回信息
        * */
        //1.
        if (dto == null) return (PageResponseResult) paramError;//向下转型
        //2.
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        Page page = new Page(dto.page,dto.size);
        IPage iPage = page(page, wrapper);
        //3.
        PageResponseResult responseResult = new PageResponseResult(dto.page,dto.size,(int)iPage.getTotal());
        responseResult.setCode(0);
        responseResult.setErrorMessage("查询成功");
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    //自媒体人审核
    @Override
//    @Transactional//事务
    @GlobalTransactional//分布式事务注解
    public ResponseResult updateStatusById(AuthDto dto) {
        //检查参数        判断dto和hto.status不为null  且 dto.status≠2   dto.status≠9    dto.getId()
        if (dto == null || dto.getId() == null || dto.getStatus() == null ||  (dto.getStatus() != UserConstants.FAIL_AUTH &&dto.getStatus() != UserConstants.PASS_AUTH) ) return paramError;
        //修改状态   根据id找到对象修改状态
        ApUserRealname realname = new ApUserRealname();
        realname.setId(dto.getId());
        realname.setStatus(dto.getStatus());
        if (dto.getMsg() != null)realname.setReason(dto.getMsg());//拒绝原因
        updateById(realname);//更新认证表的状态   本地事务
        if (dto.getStatus().equals(UserConstants.PASS_AUTH)) {
            ApUserRealname rzInfo = getById(dto.getId());//认证信息
            ApUser apUser = apUserMapper.selectById(rzInfo.getUserId());//根据认证Id找到ApUser
            WmUser wm = wemediaFeign.findByName(apUser.getName());//此处如rzInfo.getName也可、根据用户的姓名查询自媒体用户
            if (wm == null || wm.getId() == null) {
                WmUser wmUser = new WmUser();
                BeanUtils.copyProperties(apUser,wmUser);//同步共有字段  从apUsser-->wmUser
                wmUser.setApUserId(apUser.getId());//同步之后些许字段不正确  主动set
                wmUser.setCreatedTime(new Date());
                wmUser.setStatus(UserConstants.PASS_AUTH.intValue());

                wemediaFeign.save(wmUser);//至此为自媒体用户的保存--操作的数据库为 leadnews_wemedia

                //同步作者
                ApAuthor author = articleFeign.findByUserId(apUser.getId());//通过 用户Id 拿到 作者对象  用户Id为ApAuthor的外键

                if (author==null) {
                    ApAuthor apAuthor = new ApAuthor();
                    apAuthor.setName(apUser.getName());
                    apAuthor.setType(2);
                    apAuthor.setCreatedTime(new Date());
                    apAuthor.setUserId(apUser.getId());
                    WmUser newWm = wemediaFeign.findByName(apUser.getName());
                    apAuthor.setWmUserId(newWm.getId());
                    articleFeign.save(apAuthor);
                    ApAuthor newAuthor = articleFeign.findByUserId(apUser.getId());
                    newWm.setApAuthorId(newAuthor.getUserId());//作者的Id赋值给自媒体人

                    //远程调用更新自媒体用户信息
                    wemediaFeign.update(newWm);
                }

                apUser.setFlag(Short.valueOf("1"));//Flag 用户标识(自媒体人、大V 等)
                apUserMapper.updateById(apUser);//更新C端用户表的状态
//                int a = 1/0;

            }
            return ResponseResult.okResult(0,"认证通过");
        }
        return ResponseResult.okResult(0,"拒绝通过");
    }
}
